<?php
error_reporting(0);
include_once dirname(__FILE__)."/ImageHandler.php";
class ImageThumb
{
    public function __construct()
    {

    }

    public function index()
    {
        $dir = dirname(__FILE__).'/';
        $width = isset($_REQUEST['width']) ? $_REQUEST['width'] : '';
        $height = isset($_REQUEST['height']) ? $_REQUEST['height'] : '';
        $imgdir = isset($_REQUEST['imgdir']) ? $_REQUEST['imgdir'] : '';
        $filename = isset($_REQUEST['width']) ? $_REQUEST['filename'] : '';
        $ext = isset($_REQUEST['ext']) ? $_REQUEST['ext'] : '';
        if (!is_numeric($width) || !is_numeric($height) || empty($imgdir) || empty($filename) || empty($ext))
        {
            $this->Not();
        }
        $imgdir ="{$imgdir}/{$filename}.{$ext}";
        $arr_pathinfo = pathinfo($imgdir);
        $imgdir = trim($imgdir,'/');
        if(!file_exists($dir.$imgdir))
        {
            $this->Not();
        }
        $str_dirname = str_replace('\\','/',$arr_pathinfo['dirname']);
        $str_dirname = str_replace('//','/',$str_dirname);
        $str_dirname = str_replace('//','/',$str_dirname);
        $str_dirname = trim($str_dirname,'/');
        if(strlen($str_dirname) <1)
        {
            $this->Not();
        }
        $arr_dirname = explode('/',$str_dirname);
        if (count($arr_dirname) <2)
        {
            $this->Not();
        }
        array_shift($arr_dirname);

        $newdir = $dir .'thumb/'. implode('/',$arr_dirname)."/{$width}x{$height}/".$arr_pathinfo['basename']; //储存目录

        if(file_exists($newdir))
        {
            $this->img($newdir);
        }
        $result_mak_dir = $this->sysMakeConfigDir($newdir);
        if($result_mak_dir['ret'] !=0)
        {
            $this->Not();
        }
        $img = new ImageHandler();
        $img->setSrcImg($dir.$imgdir);
        $img->setDstImg($newdir);
        $img->setCutType('2'); //生成图片的类型 1手动生成 / 0 自动缩放
        $img->setImgDisplayQuality('95'); //图片质量
        $img->setRectangleCut($width, $height);
        if ($img->createImg($width, $height))
        {
            $this->imgOk($newdir);
        }
        else
        {
            $this->Not();
        }
    }

    public function sysMakeConfigDir($file_path)
    {
        $file_path = str_replace(' ','',$file_path);
        $file_path = str_replace('\\','/',$file_path);
        $file_path = str_replace('//','/',$file_path);
        if(strlen($file_path) <1){
            return $this->sysReturnData(1,"文件路径为空：{$file_path}");
        }
        $arr_pathinfo = pathinfo($file_path);
        $file_basename = isset($arr_pathinfo['basename']) ? $arr_pathinfo['basename'] : '';
        if(!isset($arr_pathinfo['dirname']) || strlen($arr_pathinfo['dirname']) <1)
        {
            return $this->sysReturnData(0,"文件路径为空：{$file_path}");
        }
        $base_file_dir = $arr_pathinfo['dirname'];
        if (is_dir($base_file_dir))
        {
            return $this->sysReturnData(0,"ok",array('base_dir'=>$file_path,'absolute_path'=>str_replace('//','/',$base_file_dir.'/'.$file_basename)));
        }
        $result=@mkdir($base_file_dir, 0777, true);
        if(!$result)
        {
            return $this->sysReturnData(1,"创建文件夹失败：{$base_file_dir}",array('base_dir'=>$file_path,'absolute_path'=>str_replace('//','/',$base_file_dir.'/'.$file_basename)));
        }
        return $this->sysReturnData(0,"ok",array('base_dir'=>$file_path,'absolute_path'=>str_replace('//','/',$base_file_dir.'/'.$file_basename)));
    }

    public function sysReturnData($ret,$reason,$data=null,$ex_data=null){
        return ['ret'=>$ret,'reason'=>$reason,'data'=>$data,'ex_data'=>$ex_data];
    }

    public function Not()
    {
        header('HTTP/1.1 404 Image Not Found');
        exit;
    }


    public function img($dir)
    {
        $img_info = @getimagesize($dir);
        $type = isset($img_info['mime']) ? $img_info['mime'] : ' image/jpg';
        Header("Content-type: $type");
        echo file_get_contents($dir);
        exit;
    }

    public function imgOk($dir)
    {
        echo file_get_contents($dir);
        exit;
    }

    /**
     * Notes:裁剪图片函数.
     * User: pan.liang
     * Date: 2020-07-21
     * Time: 17:14
     * @param $src 原图路径
     * @param $dst 目标路径
     * @param int $width 宽
     * @param int $height 高
     * @param bool $crop 是否需要裁剪
     * @throws \ImagickException
     */
    public function resize($src, $dst, $width = 320, $height = 320, $crop = false)
    {
        $imagick = new Imagick($src);
        $w = $imagick->getImageWidth();
        $h = $imagick->getImageHeight();

        if ($w > $width || $h > $height)
        {
            if ($crop)
            {
                $imagick->cropThumbnailImage($width, $height);
            }
            else
            {
                $imagick->resizeImage($width, $height, Imagick::FILTER_CATROM, 1, true);
            }
        }
        $imagick->setImageFormat('JPEG');
        $imagick->setImageCompression(Imagick::COMPRESSION_UNDEFINED);
        $a = $imagick->getImageCompressionQuality() * 0.75;
        if ($a == 0)
        {
            $a = 75;
        }
        $imagick->setImageCompressionQuality($a);
        $imagick->stripImage();
        $imagick->writeImage($dst);
        $imagick->clear();
        $imagick->destroy();
    }
}
$obj_image_thumb = new ImageThumb();
$obj_image_thumb->index();
